package cn.mall.product.service.impl;

import cn.mall.common.utils.PageUtils;
import cn.mall.common.utils.Query;
import cn.mall.product.dao.CategoryDao;
import cn.mall.product.entity.CategoryEntity;
import cn.mall.product.service.CategoryService;
import cn.mall.product.vo.Catalog2Vo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
// 1 查询出所有的分类
        List<CategoryEntity> allCategory= baseMapper.selectList(null);
// 2 封装称树形结构
//2.1 找到所有的parent_cid = 0 的分类
        List<CategoryEntity> firstLevelCategory = allCategory.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .peek(categoryEntity ->
                        categoryEntity.setChildCategory(getChileCategoryList(categoryEntity,allCategory)))
                .sorted((category1,category2)->category1.getSort()==null?
                        0:category1.getSort()-(category2.getSort()==null?0:category2.getSort()))
                .collect(Collectors.toList());
        return firstLevelCategory;
    }

    @Override
    public List<CategoryEntity> getLevel1Category() {
        List<CategoryEntity> categoryEntities= baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",0));

        return categoryEntities;
    }

    @Override
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        List<CategoryEntity> level1Category = getLevel1Category();
        Map<String,List<Catalog2Vo>> map = level1Category.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v ->{
            List<CategoryEntity> level2 = this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",v.getCatId()));
            List<Catalog2Vo> catalog2Vos = null;
            if(level2 != null){
                catalog2Vos = level2.stream().map(l2 -> {
                    Catalog2Vo catalog2Vo = new Catalog2Vo(v.getCatId().toString(),l2.getCatId().toString(),l2.getName(),null);
                    List<CategoryEntity> level3 = this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",l2.getCatId()));
                    List<Catalog2Vo.Catalog3List> catalog3Lists = level3.stream().map(item -> {
                        Catalog2Vo.Catalog3List catalog3List = new Catalog2Vo.Catalog3List(l2.getCatId().toString(),item.getCatId().toString(),item.getName());
                        return  catalog3List;
                    }).collect(Collectors.toList());
                    catalog2Vo.setCatalog3List(catalog3Lists);
                    return catalog2Vo;
                }).collect(Collectors.toList());
            }
            return catalog2Vos;
        }));
        return map;
    }

    // 递归根据⽗类查找该⽗类下的所有的⼦分类
    private List<CategoryEntity> getChileCategoryList(CategoryEntity
                                                              root,List<CategoryEntity> all){
        List<CategoryEntity> childCategoryList = all.stream().filter(categoryEntity ->
                        categoryEntity.getParentCid().equals(root.getCatId()))
                .peek(categoryEntity ->
                        categoryEntity.setChildCategory(getChileCategoryList(categoryEntity,all)))
                .sorted((category1,category2)->category1.getSort()==null?
                        0:category1.getSort()-(category2.getSort()==null?0:category2.getSort()))
                .collect(Collectors.toList());
        return childCategoryList.isEmpty()?null:childCategoryList;
    }

}